//
//  MortgageCell.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/6/16.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import UIKit
class MortgageCell: UITableViewCell {
    var carType,mileage,cardCity,cardTime,carOwner,ownerPhone:UITextField!
    let labelW:CGFloat = 60
    var submitTradeBtn:UIButton!
    var remark:UITextView!
    var boardH:CGFloat = 0.0
    var tradeView:UIView!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCellSelectionStyle.none
        self.layer.borderWidth = 0
        self.setMainView()
        //监听键盘弹出通知
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        //监听键盘隐藏通知
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setMainView() {
        tradeView = UIView.init(frame:CGRect(x: 0, y: 0, width: kScreenW, height: 10*cellH + 10))
        self.addSubview(tradeView)
        let carTypeLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: 0, width: labelW, height: cellH))
        carTypeLabel.font = oFontSize
        carTypeLabel.text = "车辆类型"
        carTypeLabel.textAlignment = .right
        tradeView.addSubview(carTypeLabel)
        
        carType = UITextField.init(frame: CGRect(x: labelW + 20, y: 0, width: kScreenW - labelW - 20, height: cellH))
        carType.textColor = fontColor
        carType.font = oFontSize
        carType.clearButtonMode = .whileEditing
        carType.placeholder = "请输入车型"
        carType.autocapitalizationType = .none //关闭首字母自动大写
        carType.inputAccessoryView = AddToolBar()
        tradeView.addSubview(carType)
        tradeView.addSubview(getSeparator(index: 1))
        
        let mileageLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 1), width: labelW, height: cellH))
        mileageLabel.font = oFontSize
        mileageLabel.text = "行驶里程"
        mileageLabel.textAlignment = .right
        tradeView.addSubview(mileageLabel)
        
        mileage = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 1), width: kScreenW - labelW - 20, height: cellH))
        mileage.textColor = fontColor
        mileage.font = oFontSize
        mileage.clearButtonMode = .whileEditing
        mileage.placeholder = "请输入行驶里程(公里)"
        mileage.autocapitalizationType = .none //关闭首字母自动大写
        mileage.inputAccessoryView = AddToolBar()
        tradeView.addSubview(mileage)
        tradeView.addSubview(getSeparator(index: 2))
        
        let cardCityLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 2), width: labelW, height: cellH))
        cardCityLabel.font = oFontSize
        cardCityLabel.text = "上牌城市"
        cardCityLabel.textAlignment = .right
        tradeView.addSubview(cardCityLabel)
        
        cardCity = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 2), width: kScreenW - labelW - 20, height: cellH))
        cardCity.textColor = fontColor
        cardCity.font = oFontSize
        cardCity.clearButtonMode = .whileEditing
        cardCity.placeholder = "请输入上牌城市"
        cardCity.autocapitalizationType = .none //关闭首字母自动大写
        cardCity.inputAccessoryView = AddToolBar()
        tradeView.addSubview(cardCity)
        tradeView.addSubview(getSeparator(index: 3))
        
        let cardTimeLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 3), width: labelW, height: cellH))
        cardTimeLabel.font = oFontSize
        cardTimeLabel.text = "上牌时间"
        cardTimeLabel.textAlignment = .right
        tradeView.addSubview(cardTimeLabel)
        
        cardTime = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 3), width: kScreenW - labelW - 20, height: cellH))
        cardTime.textColor = fontColor
        cardTime.font = oFontSize
        cardTime.clearButtonMode = .whileEditing
        cardTime.placeholder = "请选择上牌时间"
        cardTime.autocapitalizationType = .none //关闭首字母自动大写
        cardTime.inputAccessoryView = AddToolBar()
        tradeView.addSubview(cardTime)
        tradeView.addSubview(getSeparator(index: 4))
        
        let carOwnerLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 4), width: labelW, height: cellH))
        carOwnerLabel.font = oFontSize
        carOwnerLabel.text = "车主姓名"
        carOwnerLabel.textAlignment = .right
        tradeView.addSubview(carOwnerLabel)
        
        carOwner = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 4), width: kScreenW - labelW - 20, height: cellH))
        carOwner.textColor = fontColor
        carOwner.font = oFontSize
        carOwner.clearButtonMode = .whileEditing
        carOwner.placeholder = "请输入车主姓名"
        carOwner.autocapitalizationType = .none //关闭首字母自动大写
        carOwner.inputAccessoryView = AddToolBar()
        tradeView.addSubview(carOwner)
        tradeView.addSubview(getSeparator(index: 5))
        
        let ownerPhoneLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 5), width: labelW, height: cellH))
        ownerPhoneLabel.font = oFontSize
        ownerPhoneLabel.text = "联系电话"
        ownerPhoneLabel.textAlignment = .right
        tradeView.addSubview(ownerPhoneLabel)
        
        ownerPhone = UITextField.init(frame: CGRect(x: labelW + 20, y: getCellY(index: 5), width: kScreenW - labelW - 20, height: cellH))
        ownerPhone.textColor = fontColor
        ownerPhone.font = oFontSize
        ownerPhone.clearButtonMode = .whileEditing
        ownerPhone.placeholder = "请输入联系电话"
        ownerPhone.autocapitalizationType = .none //关闭首字母自动大写
        ownerPhone.inputAccessoryView = AddToolBar()
        tradeView.addSubview(ownerPhone)
        tradeView.addSubview(getSeparator(index: 6))
        
        let remarkLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: 6), width: oLabelW, height: 2*cellH))
        remarkLabel.font = oFontSize
        remarkLabel.text = "备注信息"
        tradeView.addSubview(remarkLabel)
        
        remark = UITextView(frame:CGRect(x:oLabelW, y:getCellY(index: 6) + 10, width:oImgW, height:remarkH))
        remark.layer.cornerRadius = 5
        remark.backgroundColor = tableBgColor
        remark.textColor = fontColor
        remark.font = oFontSize
        remark.delegate = self
        remark.inputAccessoryView = AddToolBar()
        tradeView.addSubview(remark)
        tradeView.addSubview(getSeparator(index: 8))
        
        submitTradeBtn = UIButton(frame:CGRect(x: oLabelLeft, y: getCellY(index: 9), width: kScreenW - 2*oLabelLeft, height: cellH))
        submitTradeBtn.backgroundColor = appMainColor
        submitTradeBtn.showsTouchWhenHighlighted = true
        submitTradeBtn.layer.cornerRadius = 10;
        tradeView.addSubview(submitTradeBtn)
    }
}
extension MortgageCell{
    func getCellY(index : Int) -> CGFloat {
        return cellH * CGFloat(index)
    }
    func getSeparator(index: Int) -> UILabel {
        let lab = UILabel.init(frame: CGRect(x: oLabelLeft, y: getCellY(index: index), width: kScreenW - 2*oLabelLeft, height: 1))
        lab.backgroundColor = separatorColor
        return lab
    }
    //键盘显示调用
    func keyboardWillShow(_ notification:Notification) {
        if currentTagY > 0 {
            let userInfo = (notification as Notification).userInfo!
            let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
            let statusBarOrientation = UIApplication.shared.statusBarOrientation
            var keyboardHeight:CGFloat = 0.0
            if UIInterfaceOrientationIsPortrait(statusBarOrientation) {
                keyboardHeight = keyboardSize.height
            }else{
                keyboardHeight = keyboardSize.width
            }
            let difference = kScreenH - kStatusBarH - kNavigationBarH - currentTagY - keyboardHeight
            if difference < 0 {
                if difference < -keyboardHeight {
                    self.frame.origin.y = -keyboardHeight
                }else{
                    self.frame.origin.y = difference
                }
            }
        }
    }
    //键盘隐藏调用
    func keyboardWillHide(_ notification:Notification) {
        currentTagY = 0
        self.frame.origin.y = currentTagY
    }
}
extension MortgageCell{
    //点击空白回收键盘
    func hideKeyBoard() {
        self.endEditing(true)
    }
    func AddToolBar() -> UIToolbar {
        let toolBar:UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: kScreenW, height: 35))
        toolBar.backgroundColor = UIColor.gray
        let spaceBtn = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let barBtn = UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(hideKeyBoard))
        toolBar.items = [spaceBtn, barBtn]
        return toolBar
    }
}
extension MortgageCell:UITextViewDelegate{
    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
        currentTagY = textView.frame.origin.y + 2*cellH
        return true
    }
}
